package com.metrics.statistics;

import static com.metrics.utils.FormatNumberUtil.*;

import java.io.Serializable;
import java.util.*;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;

import com.database.objects.*;
import com.metrics.extractors.MetricExtractor;
import com.metrics.objects.*;

public class StatisticsCalculator implements Serializable {

	private static final long serialVersionUID = 3163372920564557166L;

	public Map<Statistic, Double> calculate(Project project, MetricExtractor extractor, Type type, Status status, String start, String finish) {
		Map<Statistic, Double> statistics = new HashMap<Statistic, Double>();
		start = start != null && !start.equals("") ? start : Options.START_DATE_DEFAULT;
		finish = finish != null && !finish.equals("") ? finish : Options.FINISH_DATE_DEFAULT;

		DescriptiveStatistics elements = new DescriptiveStatistics();
		for (Developer developer : project.getDevelopers()) {
			Double value = extractor.extract(developer, type, status, start, finish);
			elements.addValue(value);
		}

		Double min = formatToTwoDecimals(elements.getMin());
		Double max = formatToTwoDecimals(elements.getMax());
		Double mean = formatToTwoDecimals(elements.getMean());
		Double stddev = formatToTwoDecimals(elements.getStandardDeviation());

		statistics.put(Statistic.MIN, min);
		statistics.put(Statistic.MAX, max);
		statistics.put(Statistic.MEAN, mean);
		statistics.put(Statistic.STDDEV, stddev);

		return statistics;
	}

}